<?php
/**
 * Created by PhpStorm.
 * User: Shinelon
 * Date: 2021/4/17
 * Time: 17:45
 */

namespace app\admin\controller;


use app\AdminRest;
use think\App;
use app\admin\model\Category;
use think\facade\View;

class GoodsCategory extends AdminRest
{
    protected $model;

    public function __construct(App $app, Category $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    public function addCategory()
    {
        try {
            validate($this->model->rule, $this->model->message)->check($this->_param);
            $data = $this->_param;
            $data['create_time'] = $data['update_time'] = time();
            $this->model->save($data);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function categoryList()
    {
        $needData = $this->_param['needData'] ?? 0;
        switch ($needData) {
            case  1:
                $keyword = '';
                if (isset($this->_param['keyword']) && !empty($this->_param['keyword']))
                    $keyword = $this->_param['keyword'];
                $where = ['deleted' => 0];
                if (isset($this->_param['status']) && !empty($this->_param['status']))
                    $where['status'] = $this->_param['status'];
                $cate = $this->model->when(!empty($keyword),
                    function ($q) use ($keyword) {
                        $q->where('name', 'like', "%$keyword%");
                    })
                    ->where($where)->order('id', 'desc')->paginate($this->_param['size'] ?? 10)->toArray();
                $one = array_filter(array_column($cate['data'],'category_id_one'));
                $two = array_filter(array_column($cate['data'],'category_id_two'));
                $parents = $this->model->whereIn('id',array_merge($one,$two))->select()->toArray();
                $parents = array_column($parents,null,'id');
                foreach ($cate['data'] as &$v){
                    $v['category_one_name'] = '';
                    $v['category_two_name'] = '';
                    if(isset($parents[$v['category_id_one']]))
                        $v['category_one_name'] = $parents[$v['category_id_one']]['name'];
                    if(isset($parents[$v['category_id_two']]))
                        $v['category_two_name'] = $parents[$v['category_id_two']]['name'];
                }
                return $this->success($cate);
                break;
            default:
                return View::fetch('categoryList');
                break;
        }
    }

    public function categoryTree()
    {
        $data = $this->model->where('level', '<>', 3)->where(['status' => 1, 'deleted' => 0])->select()->toArray();
        $data = array_column($data, null, 'id');
        foreach ($data as $d) {
            if ($d['level'] == 1 || !isset($data[$d['category_id_one']]))
                continue;
            $data[$d['category_id_one']]['child'][] = $d;
            unset($data[$d['id']]);
        }
        return $this->success(array_values($data));

    }

    public function editCategory()
    {
        $data = $this->_param;
        unset($data['id']);
        if (empty($this->_param['id']) || $this->_param['id'] < 0)
            return $this->error('数据有误');
        try {
            validate($this->model->rule, $this->model->message)->check($data);
            $data['update_time'] = time();
            if (isset($data['deleted']) && $data['deleted'] == 1) {
                $child = $this->model->where(['category_id_one' => $this->_param['id']])->whereOr(['category_id_two' => $this->_param['id']])->select()->toArray();
                if (!empty($child))
                    return $this->error('存在下级分类不能删除');
                $data['delete_time'] = $data['update_time'];
            }
            unset($data['create_time']);
            $this->model->where(['id' => $this->_param['id']])->save($data);
            return $this->success('修改成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
